﻿using JuCheap.Core.Infrastructure;
using JuCheap.Core.IService;
using JuCheap.Core.IService.AppService;
using JuCheap.Core.Model;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JuCheap.Core.WebApi.Controllers
{
    /// <summary>
    /// 产品服务api
    /// </summary>
    [Route("api/[controller]")]
    public class ProductController : BaseApiController
    {
        private readonly IProductService _productService;
        private readonly IProductSaveService _productSaveService;
        private readonly IProductQueryService _productQueryService;
        private readonly IProductSpecService _productSpecService;
        private readonly IProductImageService _productImageService;

        /// <summary>
        /// ctor
        /// </summary>
        public ProductController(IProductService productService,
            IProductSaveService productSaveService,
            IProductQueryService productQueryService,
            IProductSpecService productSpecService,
            IProductImageService productImageService)
        {
            _productService = productService;
            _productSaveService = productSaveService;
            _productQueryService = productQueryService;
            _productSpecService = productSpecService;
            _productImageService = productImageService;
        }

        /// <summary>
        /// 创建产品信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("create")]
        public async Task<ActionResult<string>> Create([FromBody]ProductCreateRequestDto req)
        {
            var id = await _productSaveService.CreateAsync(req);
            return Ok(id);
        }

        /// <summary>
        /// 分页获取产品信息
        /// </summary>
        [HttpPost]
        [Route("search")]
        public async Task<ActionResult<PagedResult<ProductDto>>> Search([FromBody]ProductFilter filter)
        {
            var records = await _productQueryService.QueryPagesAsync(filter);
            return Ok(records);
        }

        /// <summary>
        /// 获取产品信息
        /// </summary>
        [HttpGet]
        [Route("{id}")]
        public async Task<ActionResult<ProductDto>> Get([FromRoute]string id)
        {
            var productDto = await _productService.FindAsync(id);
            if(productDto != null)
            {
                productDto.Specs = await _productSpecService.FindListAsync(x => x.ProductId == id);
                productDto.Images = await _productImageService.FindListAsync(x => x.ProductId == id);
                //排序，保证默认图片排在首位
                productDto.Images = productDto.Images.OrderByDescending(x => x.IsDefault).ToList();
            }
            return Ok(productDto);
        }

        /// <summary>
        /// 修改产品信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("edit")]
        public async Task<ActionResult<bool>> Edit([FromBody]ProductEditRequestDto req)
        {
            await _productSaveService.UpdateAsync(req);
            return Ok(true);
        }

        /// <summary>
        /// 删除产品信息
        /// </summary>
        /// <param name="ids">id集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("delete")]
        public async Task<ActionResult<bool>> Delete([FromBody]IList<string> ids)
        {
            await _productService.DeleteAsync(ids);
            return Ok(true);
        }
    }
}
